package cn.edu.zafu.bigdata.x.zhaobolei.Seventh;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class Student implements Serializable {
    private static final long serialVersionUID = 1L;
    private String id;
    String name;
    String phone;

    public Student(String id, String name, String phone) {
        this.id = id;
        this.name = name;
        this.phone = phone;
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getPhone() {
        return phone;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", phone='" + phone + '\'' +
                '}';
    }
}

public class StudentInformationManagementSystem {
    private static final String FILE_NAME = "src\\main\\java\\cn\\edu\\zafu\\bigdata\\x\\zhaobolei\\Seventh\\students.dat";
    private static List<Student> students = new ArrayList<>();

    public static void main(String[] args) {
        loadStudentsFromFile();
        Scanner scanner = new Scanner(System.in);
        while (true) {
            showMenu();
            int choice = getUserChoice(scanner);
            switch (choice) {
                case 1:
                    addStudent(scanner);
                    break;
                case 2:
                    searchStudent(scanner);
                    break;
                case 3:
                    updateStudent(scanner);
                    break;
                case 4:
                    deleteStudent(scanner);
                    break;
                case 5:
                    saveStudentsToFile();
                    System.out.println("程序已退出！");
                    System.exit(0);
                default:
                    System.out.println("无效的选择，请重新输入！");
            }
        }
    }

    private static void showMenu() {
        System.out.println("======= 学生信息管理系统 =======");
        System.out.println("1. 添加学生信息");
        System.out.println("2. 检索学生信息");
        System.out.println("3. 更新学生信息");
        System.out.println("4. 删除学生信息");
        System.out.println("5. 退出程序");
        System.out.print("请输入操作编号 (1-5): ");
    }

    private static int getUserChoice(Scanner scanner) {
        while (true) {
            try {
                int choice = Integer.parseInt(scanner.nextLine());
                if (choice >= 1 && choice <= 5) {
                    return choice;
                } else {
                    System.out.println("请输入1-5之间的数字，请重新输入！");
                }
            } catch (NumberFormatException e) {
                System.out.println("输入格式错误，请重新输入数字！");
            }
        }
    }

    private static void addStudent(Scanner scanner) {
        System.out.print("请输入学号: ");
        String id = scanner.nextLine();
        if (isIdExists(id)) {
            System.out.println("学号已存在，请重新输入！");
            return;
        }
        System.out.print("请输入姓名: ");
        String name = scanner.nextLine();
        System.out.print("请输入电话: ");
        String phone = scanner.nextLine();
        Student student = new Student(id, name, phone);
        students.add(student);
        System.out.println("学生信息添加成功！");
    }

    private static boolean isIdExists(String id) {
        for (Student student : students) {
            if (student.getId().equals(id)) {
                return true;
            }
        }
        return false;
    }

    private static void searchStudent(Scanner scanner) {
        System.out.println("请输入要检索的学号（结束检索请输入QUIT）:");
        while (true) {
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                break;
            }
            Student student = findStudentById(id);
            if (student!= null) {
                System.out.println(student);
            } else {
                System.out.println("未找到该学生");
            }
        }
    }

    private static Student findStudentById(String id) {
        for (Student student : students) {
            if (student.getId().equals(id)) {
                return student;
            }
        }
        return null;
    }

    private static void updateStudent(Scanner scanner) {
        System.out.println("请输入要更新信息的学号（结束更新请输入QUIT）:");
        while (true) {
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                break;
            }
            Student student = findStudentById(id);
            if (student!= null) {
                System.out.print("请输入新的姓名: ");
                String newName = scanner.nextLine();
                System.out.print("请输入新的电话: ");
                String newPhone = scanner.nextLine();
                student.name = newName;
                student.phone = newPhone;
                System.out.println("学生信息更新成功！");
            } else {
                System.out.println("未找到该学生");
            }
        }
    }

    private static void deleteStudent(Scanner scanner) {
        System.out.println("请输入要删除的学号（结束删除请输入QUIT）:");
        while (true) {
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                break;
            }
            Student student = findStudentById(id);
            if (student!= null) {
                students.remove(student);
                System.out.println("学生信息删除成功！");
            } else {
                System.out.println("未找到该学生");
            }
        }
    }

    private static void loadStudentsFromFile() {
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILE_NAME))) {
            students = (List<Student>) ois.readObject();
        } catch (IOException | ClassNotFoundException e) {
            // 文件不存在或者读取失败时，students列表为空，后续可以正常添加新学生
        }
    }

    private static void saveStudentsToFile() {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(FILE_NAME))) {
            oos.writeObject(students);
        } catch (IOException e) {
            System.out.println("保存学生信息到文件时出错: " + e.getMessage());
        }
    }
}
